package org.thanos.simulator.page.network;

import org.thanos.simulator.api.BACnetIpClient;
import org.thanos.simulator.page.IndexPage;
import org.thanos.simulator.util.NetworkUtil;
import org.thanos.simulator.util.TimerUtil;

import javax.swing.*;
import javax.swing.text.NumberFormatter;
import java.awt.*;
import java.text.NumberFormat;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class NetworkConfigPage {

    private final JComboBox<String> comboBox;
    private final JTextField port;
    private final JTextField deviceId;
    private final JTextField networkNumber;
    private final JTextField deviceName;

    private final JTextField interval;

    public NetworkConfigPage(IndexPage indexPage) {
        // 创建 JFrame
        JFrame frame = new JFrame("本地网络配置");
        frame.setLocation(GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint());
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(new GridBagLayout()); // 使用 GridBagLayout

        // 创建 GridBagConstraints
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5, 5, 5, 5); // 设置组件之间的间距

        // 创建下拉框
        List<String> options = NetworkUtil.getNetworkInterfaces().stream().map(NetworkUtil.NetworkInterfaceDTO::getIpAddress).collect(Collectors.toList());
        comboBox = new JComboBox<>(options.toArray(new String[]{}));
        comboBox.setPreferredSize(new Dimension(150, 30)); // 设置尺寸为 150x30 像素
        frame.add(new JLabel("选择Ip地址:"), gbc); // 添加标签
        gbc.gridx++;
        frame.add(comboBox, gbc);

        // 创建数字输入框
        NumberFormat format = NumberFormat.getInstance();
        NumberFormatter formatter = new NumberFormatter(format);
        formatter.setValueClass(Integer.class);
        formatter.setMinimum(0);
        formatter.setMaximum(Integer.MAX_VALUE);

        gbc.gridx = 0;
        gbc.gridy++;
        frame.add(new JLabel("端口:"), gbc); // 添加标签
        gbc.gridx++;
        port = new JFormattedTextField(formatter);
        port.setText("47808");
        port.setPreferredSize(new Dimension(150, 30)); // 设置尺寸为 150x30 像素
        frame.add(port, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        frame.add(new JLabel("设备地址:"), gbc); // 添加标签
        gbc.gridx++;
        deviceId = new JFormattedTextField(formatter);
        deviceId.setPreferredSize(new Dimension(150, 30)); // 设置尺寸为 150x30 像素
        frame.add(deviceId, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        frame.add(new JLabel("网络号:"), gbc); // 添加标签
        gbc.gridx++;
        networkNumber = new JFormattedTextField(formatter);
        networkNumber.setText("0");
        networkNumber.setPreferredSize(new Dimension(150, 30)); // 设置尺寸为 150x30 像素
        frame.add(networkNumber, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        frame.add(new JLabel("设备名称:"), gbc); // 添加标签
        gbc.gridx++;
        deviceName = new JTextField();
        deviceName.setText("Demo");
        deviceName.setPreferredSize(new Dimension(150, 30)); // 设置尺寸为 150x30 像素
        frame.add(deviceName, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        frame.add(new JLabel("数值刷新周期:"), gbc); // 添加标签
        gbc.gridx++;
        interval = new JFormattedTextField(formatter);
        interval.setText("1");
        interval.setToolTipText("单位（秒）");
        interval.setPreferredSize(new Dimension(150, 30)); // 设置尺寸为 150x30 像素
        frame.add(interval, gbc);

        // 创建确认按钮
        gbc.gridx = 0;
        gbc.gridy++;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        JButton button = new JButton("确认");
        button.addActionListener(e -> {
            try {
                int portValue = NumberFormat.getIntegerInstance().parse(port.getText()).intValue();
                int deviceIdValue = NumberFormat.getIntegerInstance().parse(deviceId.getText()).intValue();
                int networkNumberValue = NumberFormat.getIntegerInstance().parse(networkNumber.getText()).intValue();
                int intervalValue = NumberFormat.getIntegerInstance().parse(interval.getText()).intValue();
                String ip = Objects.requireNonNull(comboBox.getSelectedItem()).toString();
                String deviceNameText = deviceName.getText();
                BACnetIpClient.init(ip, networkNumberValue, portValue, deviceIdValue, deviceNameText, false, intervalValue);
                TimerUtil.run(intervalValue, indexPage);
                indexPage.reloadTree();
                indexPage.reloadMenu();
                frame.dispose();
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
                throw new RuntimeException(ex);
            }
        });
        frame.add(button, gbc);
        frame.pack(); // 自适应大小
        frame.setVisible(true);
    }
}
